<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="showDialog=true"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="list">
              <el-table-column label="序号" width="120" type="index" align="center" />
              <el-table-column label="角色名称" align="center" width="240" prop="name" />
              <el-table-column label="描述" align="center" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="{row}">
                  <el-button size="small" type="success" @click="assignPermission(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="deleteRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <!-- <el-pagination layout="prev,pager,next,total,sizes" :current-page="page.page" :page-size="page.pagesize" :total="page.total" @current-change="changePage" /> -->
              <el-pagination
                :current-page="page.page"
                :page-size="page.pagesize"
                layout="prev, pager, next, jumper"
                :total="page.total"
                @current-change="changePage"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="formData.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="formData.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="formData.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="formData.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>

      <!-- 对话框新增和编辑复用 -->
      <el-dialog
        :title="title"
        :visible="showDialog"
        @close="btnCancel"
        @open="showTitle"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px" class="demo-roleForm">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="region">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnCancel">取消</el-button>
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <!-- 权限 -->
      <el-dialog title="分配权限" :visible="showPermDialog" @close="btnPermCancel">
        <!-- 权限是一颗树 -->
        <!-- 将数据绑定到组件上 -->
        <!-- check-strictly 如果为true 那表示父子勾选时  不互相关联 如果为false就互相关联 -->
        <!-- id作为唯一标识 -->
        <!-- :default-checked-keys="selectCheck" -->
        <el-tree
          ref="permTree"
          :data="permData"
          :props="defaultProps"
          :show-checkbox="true"
          :check-strictly="true"
          :default-expand-all="true"
          node-key="id"
        />
        <!-- 确定 取消 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button type="primary" size="small" @click="btnPermOK">确定</el-button>
            <el-button size="small" @click="btnPermCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRoleList, getCompanyInfo, deleteRole, getRoleDetail, updateRole, addRole, assignPrem } from '@/api/setting'
import { mapGetters } from 'vuex'
import { ListToTreeData } from '@/utils'
import { getPermissionList } from '@/api/permission'
export default {
  data() {
    return {
      list: [],
      page: {
        page: 1,
        pagesize: 2,
        total: 0 // 记录总数
      },
      formData: {},
      showDialog: false,
      roleForm: {}, // 接收新增或者编辑的表单数据
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }] // 校验规则
      },
      title: '',
      showPermDialog: false, // 控制分配权限弹层的显示后者隐藏
      defaultProps: {
        label: 'name'
      },
      permData: [], // 专门用来接收权限数据 树形数据
      selectCheck: [], // 定义一个数组来接收 已经选中的节点
      roleId: null // 用来记录分配角色的id
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    async getRoleList() {
      const { rows, total } = await getRoleList(this.page)
      this.page.total = total
      this.list = rows
    },
    async getCompanyInfo() {
      console.log(91, this)
      this.formData = await getCompanyInfo(this.companyId)
    },
    changePage(page) {
      console.log(page)// 当前点击的页面
      this.page.page = page
      this.getRoleList()
    },
    async deleteRole(id) {
      // console.log(id)
      try {
        await this.$confirm('确认删除该角色吗')
        // 只有确定了，才能继续下一步
        await deleteRole(id)
        // console.log(res)
        this.getRoleList() // 重新加载数据
        this.$message.success('删除角色成功')
      } catch (err) {
        // console.log(err)
      }
    },
    async editRole(id) {
      console.log(id)
      this.roleForm = await getRoleDetail(id) // 获取具体数据回写
      this.showDialog = true // 为了不出现闪烁的问题，先获取数据，再弹出框
    },
    btnCancel() {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()// 要重置表单
      this.showDialog = false
    },
    async btnOK() {
      try {
        await this.$refs.roleForm.validate()// 先通过校验,再执行下面代码
        if (this.roleForm.id) {
        // 有id就是编辑
          await updateRole(this.roleForm.id, this.roleForm)
        // console.log(res)
        } else {
        // 新增
          await addRole(this.roleForm)
        // console.log(res)
        }
        // 重新渲染数据
        this.getRoleList()
        this.$message.success('操作成功')
        this.showDialog = false
      } catch (err) {
        console.log(err)
      }
    },
    showTitle() {
      console.log(174, this.roleForm)
      if (this.roleForm.id) this.title = '编辑角色'
      else this.title = '新增角色'
    },
    // 点击分配权限
    async assignPermission(id) {
      this.permData = ListToTreeData(await getPermissionList(), '0') // 转换成树形数据
      this.roleId = id
      // this.selectCheck = permIds
      const { permIds } = await getRoleDetail(id) // 当前角色所拥有的权限点数据
      console.log(228, permIds)
      this.showPermDialog = true
      this.$nextTick(() => {
        this.$refs.permTree.setCheckedKeys(permIds)
      })
    },
    async btnPermOK() {
      console.log(1)
      await assignPrem({ permIds: this.$refs.permTree.getCheckedKeys(), id: this.roleId })
      this.$message.success('分配权限成功')
      this.showPermDialog = false
    },
    btnPermCancel() {
      // this.selectCheck = []// 重置数据
      this.$refs.permTree.setCheckedKeys([])
      this.showPermDialog = false
    }
  }

}
</script>

<style>

</style>
